package com.appspot.thinkhea;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.appspot.thinkhea.model.MyDBHelper;
import com.appspot.thinkhea.model.Picture;
import com.appspot.thinkhea.view.PlayerNameAdapter;

public class PanelRandomTeamView extends Activity {
	private MyDBHelper db;
	private ListView v, n, c;
	private String tag = "PanelRandomPicture";

	private int players = 0;
	private int players_per_team = 0;
	private int teams = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setBackgroundDrawableResource(R.drawable.bg);
		setTitle(R.string.desc_random_picture);

		Bundle bundle = this.getIntent().getExtras();
		players = bundle.getInt("PLAYERS");
		players_per_team = bundle.getInt("PLAYER_PER_TEAM");
		teams = bundle.getInt("TEAM");

		if (teams == 2) {
			setContentView(R.layout.panel_list_random_2_team);
		} else {
			setContentView(R.layout.panel_list_random_3_team);
		}

		// handle list event and model
		v = (ListView) findViewById(R.id.Team1ListView);
		v.setFocusable(true);
		v.setItemsCanFocus(true);
		v.setClickable(true);
		v.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		// handle list event and model
		n = (ListView) findViewById(R.id.Team2ListView);
		n.setFocusable(true);
		n.setItemsCanFocus(true);
		n.setClickable(true);
		n.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		// handle list event and model
		c = (ListView) findViewById(R.id.Team3ListView);
		c.setFocusable(true);
		c.setItemsCanFocus(true);
		c.setClickable(true);
		c.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		loadPictures();

		// Add Button Listener
		Button b = (Button) findViewById(R.id.BtnGoBack);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putInt("PLAYERS", players);
				bundle.putInt("PLAYER_PER_TEAM", players_per_team);
				bundle.putInt("TEAM", teams);
				Intent myIntent = new Intent(PanelRandomTeamView.this,
						PanelRandomTeam.class);
				myIntent.putExtras(bundle);
				PanelRandomTeamView.this.startActivity(myIntent);

			}
		});

	}

	protected long savePictures(Vector<Picture> pics) {
		if (db == null) {
			db = new MyDBHelper(this);
		}
		db.open();
		long result = -1;
		try {
			for (int b = 0; b < pics.size(); b++) {
				Picture f = pics.get(b);
				f.setRate(1);
				db.updatePicutre(f);
			}
		} catch (SQLException e) {
			Log.e(tag, "SQLException - addRecord");
		}
		db.close();
		return result;
	}

	protected void loadPictures() {
		Vector<Picture> p = null;

		if (db == null) {
			db = new MyDBHelper(this);
		}
		db.open();
		p = db.getRatedPicture(1);
		db.close();

		Vector<Picture> team1 = new Vector<Picture>();
		Vector<Picture> team2 = new Vector<Picture>();
		Vector<Picture> team3 = new Vector<Picture>();

		randomTeam(p, team1, team2, team3);

		PlayerNameAdapter iAdapter = new PlayerNameAdapter(this, team1);
		PlayerNameAdapter iAdapter2 = new PlayerNameAdapter(this, team2);
		PlayerNameAdapter iAdapter3 = new PlayerNameAdapter(this, team3);
		// handle list event and model
		v.setAdapter(iAdapter);
		n.setAdapter(iAdapter2);
		c.setAdapter(iAdapter3);
	}

	private void randomTeam(Vector<Picture> c, Vector<Picture> team1,
			Vector<Picture> team2, Vector<Picture> team3) {
		int size = c.size();
		if (teams == 2) {
			for (int i = 0; i < size; i++) {
				Picture a = c.get(i);
				int team = (int) ((Math.random() * 2) + 1);
				if (team == 1) {
					if (team1.size() < players_per_team) {
						team1.add(a);
					} else {
						team2.add(a);
					}
				} else {
					if (team2.size() < players_per_team) {
						team2.add(a);
					} else {
						team1.add(a);
					}
				}
			}
		} else {
			for (int i = 0; i < size; i++) {
				Picture a = c.get(i);
				boolean added = false;
				while (!added) {
					int team = (int) ((Math.random() * 3) + 1);
					switch (team) {
					case 1:
						if (team1.size() < players_per_team) {
							team1.add(a);
							added = true;
						}
						break;
					case 2:
						if (team2.size() < players_per_team) {
							team2.add(a);
							added = true;
						}
						break;
					case 3:
						if (team3.size() < players_per_team) {
							team3.add(a);
							added = true;
						}
						break;
					}
				}
			}
		}

	}
}